<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Spring-day03 | 阿毛的博客</title><meta name="keywords" content="spring"><meta name="author" content="myl"><meta name="copyright" content="myl"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="AOP1)AOP简介1.1)OOP开发思路 1.2)AOP开发思想 1.3)AOP概念 AOP(Aspect Oriented Programing)面向切面编程，一种编程范式，隶属于软工范畴，指导开发者如何组织程序结构  AOP弥补了OOP的不足，基于OOP基础之上进行横向开发  uOOP规定程序开发以类为主体模型，一切围绕对象进行，完成某个任务先构建模型  uAOP程序开发主要关注基于OOP开">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring-day03">
<meta property="og:url" content="https://gitee.com/tgreen/myl-mm.git/2022/05/09/Spring-day03/index.html">
<meta property="og:site_name" content="阿毛的博客">
<meta property="og:description" content="AOP1)AOP简介1.1)OOP开发思路 1.2)AOP开发思想 1.3)AOP概念 AOP(Aspect Oriented Programing)面向切面编程，一种编程范式，隶属于软工范畴，指导开发者如何组织程序结构  AOP弥补了OOP的不足，基于OOP基础之上进行横向开发  uOOP规定程序开发以类为主体模型，一切围绕对象进行，完成某个任务先构建模型  uAOP程序开发主要关注基于OOP开">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg">
<meta property="article:published_time" content="2022-05-09T08:57:26.736Z">
<meta property="article:modified_time" content="2022-05-09T11:35:14.065Z">
<meta property="article:author" content="myl">
<meta property="article:tag" content="spring">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg"><link rel="shortcut icon" href="/myl-mm/img/favicon.png"><link rel="canonical" href="https://gitee.com/tgreen/myl-mm.git/2022/05/09/Spring-day03/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/myl-mm/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/myl-mm/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Spring-day03',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-09 19:35:14'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/myl-mm/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/myl-mm/tags/"><div class="headline">标签</div><div class="length-num">11</div></a><a href="/myl-mm/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myl-mm/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/long1.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/myl-mm/">阿毛的博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myl-mm/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Spring-day03</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-09T08:57:26.736Z" title="发表于 2022-05-09 16:57:26">2022-05-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-09T11:35:14.065Z" title="更新于 2022-05-09 19:35:14">2022-05-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/myl-mm/categories/spring/">spring</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Spring-day03"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="AOP"><a href="#AOP" class="headerlink" title="AOP"></a>AOP</h1><h2 id="1-AOP简介"><a href="#1-AOP简介" class="headerlink" title="1)AOP简介"></a>1)AOP简介</h2><h3 id="1-1-OOP开发思路"><a href="#1-1-OOP开发思路" class="headerlink" title="1.1)OOP开发思路"></a>1.1)OOP开发思路</h3><p><img src="/myl-mm/assets%5C1591281580135.png" alt="1591281580135"></p>
<h3 id="1-2-AOP开发思想"><a href="#1-2-AOP开发思想" class="headerlink" title="1.2)AOP开发思想"></a>1.2)AOP开发思想</h3><p><img src="/myl-mm/assets%5C1591281706237.png" alt="1591281706237"></p>
<h3 id="1-3-AOP概念"><a href="#1-3-AOP概念" class="headerlink" title="1.3)AOP概念"></a>1.3)AOP概念</h3><ul>
<li><p>AOP(Aspect Oriented Programing)面向切面编程，一种编程<strong>范式</strong>，隶属于软工范畴，指导开发者如何组织程序结构</p>
</li>
<li><p>AOP弥补了OOP的不足，基于OOP基础之上进行横向开发</p>
<ul>
<li><p>uOOP规定程序开发以类为主体模型，一切围绕对象进行，完成某个任务先构建模型</p>
</li>
<li><p>uAOP程序开发主要关注基于OOP开发中的共性功能，一切围绕共性功能进行，完成某个任务先构建可能遇到的所有共性功能（当所有功能都开发出来也就没有共性与非共性之分）</p>
</li>
</ul>
</li>
<li><p>“AOP联盟”</p>
</li>
</ul>
<h3 id="1-4-AOP作用"><a href="#1-4-AOP作用" class="headerlink" title="1.4)AOP作用"></a>1.4)AOP作用</h3><ul>
<li><p>伴随着AOP时代的降临，可以从各个行业的标准化、规范化开始入手，一步一步将所有共性功能逐一开发完毕，最终以功能组合来完成个别业务模块乃至整体业务系统的开发</p>
</li>
<li><p>目标：将软件开发由手工制作走向半自动化&#x2F;全自动化阶段，实现“插拔式组件体系结构”搭建</p>
</li>
</ul>
<h3 id="1-5-AOP优势"><a href="#1-5-AOP优势" class="headerlink" title="1.5)AOP优势"></a>1.5)AOP优势</h3><ul>
<li><p>提高代码的可重用性</p>
</li>
<li><p>业务代码编码更简洁</p>
</li>
<li><p>业务代码维护更高效</p>
</li>
<li><p>业务功能扩展更便捷</p>
</li>
</ul>
<p><img src="/myl-mm/assets%5C1591281844901.png" alt="1591281844901"></p>
<h2 id="2-AOP入门案例"><a href="#2-AOP入门案例" class="headerlink" title="2)AOP入门案例"></a>2)AOP入门案例</h2><h3 id="2-1-AOP相关概念"><a href="#2-1-AOP相关概念" class="headerlink" title="2.1)AOP相关概念"></a>2.1)AOP相关概念</h3><p><img src="/myl-mm/assets%5C1591281889446.png" alt="1591281889446"></p>
<p><img src="/myl-mm/assets%5C1591281914574.png" alt="1591281914574"></p>
<p><img src="/myl-mm/assets%5C1591281945260.png" alt="1591281945260"></p>
<ul>
<li><p>Joinpoint(连接点)：就是方法</p>
</li>
<li><p>Pointcut(切入点)：就是挖掉共性功能的方法</p>
</li>
<li><p>Advice(通知)：就是共性功能，最终以一个方法的形式呈现</p>
</li>
<li><p>Aspect(切面)：就是共性功能与挖的位置的对应关系</p>
</li>
<li><p>Target(目标对象)：就是挖掉功能的方法对应的类产生的对象，这种对象是无法直接完成最终工作的</p>
</li>
<li><p>Weaving(织入)：就是将挖掉的功能回填的动态过程</p>
</li>
<li><p>Proxy(代理)：目标对象无法直接完成工作，需要对其进行功能回填，通过创建原始对象的代理对象实现</p>
</li>
<li><p>Introduction(引入&#x2F;引介) ：就是对原始对象无中生有的添加成员变量或成员方法</p>
</li>
</ul>
<h3 id="2-2-AOP开发过程"><a href="#2-2-AOP开发过程" class="headerlink" title="2.2)AOP开发过程"></a>2.2)AOP开发过程</h3><ul>
<li><p>开发阶段(开发者完成)</p>
<ul>
<li><p>正常的制作程序</p>
</li>
<li><p>将非共性功能开发到对应的目标对象类中，并制作成切入点方法</p>
</li>
<li><p>将共性功能独立开发出来，制作成<strong>通知</strong></p>
</li>
<li><p>在配置文件中，声明<strong>切入点</strong></p>
</li>
<li><p>在配置文件中，声明<strong>切入点</strong>与<strong>通知</strong>间的关系（含<strong>通知类型</strong>），即<strong>切面</strong></p>
</li>
</ul>
</li>
<li><p>运行阶段(AOP完成)</p>
<ul>
<li><p>Spring容器加载配置文件，监控所有配置的<strong>切入点</strong>方法的执行</p>
</li>
<li><p>当监控到<strong>切入点</strong>方法被运行，使用<strong>代理</strong>机制，动态创建<strong>目标对象</strong>的<strong>代理对象</strong>，根据<strong>通知类别</strong>，在<strong>代理对象</strong>的对应位置将<strong>通知</strong>对应的功能<strong>织入</strong>，完成完整的代码逻辑并运行</p>
</li>
</ul>
</li>
</ul>
<h3 id="2-2-AOP开发方式"><a href="#2-2-AOP开发方式" class="headerlink" title="2.2)AOP开发方式"></a>2.2)AOP开发方式</h3><ul>
<li><p>XML方式</p>
</li>
<li><p>XML+注解方式</p>
</li>
<li><p>注解方式</p>
</li>
</ul>
<h3 id="2-3-入门案例制作分析"><a href="#2-3-入门案例制作分析" class="headerlink" title="2.3)入门案例制作分析"></a>2.3)入门案例制作分析</h3><p>1.导入相关坐标</p>
<p>2.确认要抽取的功能，并将其制作成方法保存到专用的类中，删除原始业务中对应的功能</p>
<p>3.将所有进行AOP操作的资源加载到IoC容器中</p>
<p>4.使用配置的方式描述被抽取功能的位置，并描述被抽取功能与对应位置的关系</p>
<p>5.运行程序</p>
<p>步骤一 导入坐标</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.aspectj<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>aspectjweaver<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.9.4<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>步骤二 在业务层抽取通用代码</p>
<p><img src="/myl-mm/assets%5C1591282302976.png" alt="1591282302976"></p>
<p>步骤三 把通知加入spring容器管理</p>
<p><img src="/myl-mm/assets%5C1591282320624.png" alt="1591282320624"></p>
<p>步骤四 在配置文件中配置aop的配置 </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--aop配置--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--配置切入点--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *..*())&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--配置切面--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">        &lt;!—通知与切入点之间的关系--&gt;</span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;logAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-AOP配置（XML）"><a href="#3-AOP配置（XML）" class="headerlink" title="3)AOP配置（XML）"></a>3)AOP配置（XML）</h2><h3 id="3-1-AspectJ"><a href="#3-1-AspectJ" class="headerlink" title="3.1)AspectJ"></a>3.1)<strong>AspectJ</strong></h3><ul>
<li><p>Aspect（切面）用于描述切入点与通知间的关系，是AOP编程中的一个概念</p>
</li>
<li><p>AspectJ是基于java语言对Aspect的实现</p>
</li>
</ul>
<h3 id="3-2-AOP配置"><a href="#3-2-AOP配置" class="headerlink" title="3.2)AOP配置"></a>3.2)AOP配置</h3><h4 id="3-2-1-aop-config"><a href="#3-2-1-aop-config" class="headerlink" title="3.2.1)aop:config"></a>3.2.1)aop:config</h4><ul>
<li><p>名称：aop:config</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：beans标签</p>
</li>
<li><p>作用：设置AOP</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">beans</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:config</span>&gt;</span>……<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:config</span>&gt;</span>……<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个beans标签中可以配置多个aop:config标签</p>
</li>
</ul>
<h4 id="3-2-2-aop-aspect"><a href="#3-2-2-aop-aspect" class="headerlink" title="3.2.2)aop:aspect"></a>3.2.2)aop:aspect</h4><ul>
<li><p>名称：aop:aspect</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:config标签</p>
</li>
<li><p>作用：设置具体的AOP通知对应的切入点</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;beanId&quot;</span>&gt;</span>……<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;beanId&quot;</span>&gt;</span>……<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：</p>
<p>一个aop:config标签中可以配置多个aop:aspect标签</p>
</li>
<li><p>基本属性：</p>
<ul>
<li>ref ：通知所在的bean的id</li>
</ul>
</li>
</ul>
<h4 id="3-2-3-aop-pointcut"><a href="#3-2-3-aop-pointcut" class="headerlink" title="3.2.3)aop:pointcut"></a>3.2.3)aop:pointcut</h4><ul>
<li><p>名称：aop:pointcut</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:config标签、aop:aspect标签</p>
</li>
<li><p>作用：设置切入点</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pointcutId&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pointcutId&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：</p>
<p>一个aop:config标签中可以配置多个aop:pointcut标签，且该标签可以配置在aop:aspect标签内</p>
</li>
<li><p>基本属性：</p>
<ul>
<li><p>id ：识别切入点的名称</p>
</li>
<li><p>expression ：切入点表达式</p>
</li>
</ul>
</li>
</ul>
<h3 id="3-3-切入点"><a href="#3-3-切入点" class="headerlink" title="3.3)切入点"></a><strong>3.3)切入点</strong></h3><ul>
<li><p>切入点描述的是某个方法</p>
</li>
<li><p>切入点表达式是一个快速匹配方法描述的通配格式，类似于正则表达式</p>
</li>
</ul>
<h3 id="3-4-切入点表达式的组成"><a href="#3-4-切入点表达式的组成" class="headerlink" title="3.4)切入点表达式的组成"></a><strong>3.4)切入点表达式的组成</strong></h3><ul>
<li><p>切入点描述的是某个方法</p>
</li>
<li><p>切入点表达式是一个快速匹配方法描述的通配格式，类似于正则表达式</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">关键字（访问修饰符  返回值  包名.类名.方法名（参数）异常名）</span><br></pre></td></tr></table></figure></li>
</ul>
<p>​	关键字：描述表达式的匹配模式（参看关键字列表）</p>
<p>​	访问修饰符：方法的访问控制权限修饰符</p>
<p>​	类名：方法所在的类（此处可以配置接口名称）</p>
<p>​	异常：方法定义中指定抛出的异常</p>
<ul>
<li><p>范例：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">execution（public User com.itheima.service.UserService.findById（int））</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="3-4-1-切入点表达式——关键字"><a href="#3-4-1-切入点表达式——关键字" class="headerlink" title="3.4.1)切入点表达式——关键字"></a>3.4.1)切入点表达式——关键字</h4><ul>
<li><p>execution ：匹配执行指定方法</p>
</li>
<li><p>args ：匹配带有指定参数类型的方法</p>
</li>
<li><p>within ：…… </p>
</li>
<li><p>this ：…… </p>
</li>
<li><p>target ：…… </p>
</li>
<li><p>@within ：…… </p>
</li>
<li><p>@target ：…… </p>
</li>
<li><p>@args ：…… </p>
</li>
<li><p>@annotation ：…… </p>
</li>
<li><p>bean ：……</p>
</li>
<li><p>reference pointcut ：……</p>
</li>
</ul>
<h4 id="3-4-2-切入点表达式——通配符"><a href="#3-4-2-切入点表达式——通配符" class="headerlink" title="3.4.2)切入点表达式——通配符"></a>3.4.2)切入点表达式——<strong>通配符</strong></h4><ul>
<li><p>*：单个独立的任意符号，可以独立出现，也可以作为前缀或者后缀的匹配符出现</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">execution（public * com.itheima.*.UserService.find*（*））</span><br></pre></td></tr></table></figure></li>
</ul>
<p>​	匹配com.itheima包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法</p>
<ul>
<li><p>.. ：多个连续的任意符号，可以独立出现，常用于简化包名与参数的书写</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">execution（public User com..UserService.findById（..））</span><br></pre></td></tr></table></figure></li>
</ul>
<p>​	匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法</p>
<ul>
<li><p>+：专用于匹配子类类型</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">execution(* *..*Service+.*(..))</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="3-4-3-切入点表达式——逻辑运算符"><a href="#3-4-3-切入点表达式——逻辑运算符" class="headerlink" title="3.4.3)切入点表达式——逻辑运算符"></a>3.4.3)切入点表达式——逻辑运算符</h4><ul>
<li><p>&amp;&amp; ：连接两个切入点表达式，表示两个切入点表达式同时成立的匹配</p>
</li>
<li><p>|| ：连接两个切入点表达式，表示两个切入点表达式成立任意一个的匹配</p>
</li>
<li><p>! ：连接单个切入点表达式，表示该切入点表达式不成立的匹配</p>
</li>
</ul>
<h4 id="3-4-4-切入点表达式——范例"><a href="#3-4-4-切入点表达式——范例" class="headerlink" title="3.4.4)切入点表达式——范例"></a>3.4.4)切入点表达式——范例</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">execution(* *(..))</span><br><span class="line">execution(* *..*(..))</span><br><span class="line">execution(* *..*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> * *..*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="type">int</span> *..*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> *..*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> com..*.*(..)) </span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> com..service.*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> com.itheima.service.*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> com.itheima.service.User*.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> com.itheima.service.*Service.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> <span class="keyword">void</span> com.itheima.service.UserService.*(..))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.find*(..))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.*Id(..))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.findById(..))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.findById(<span class="type">int</span>))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.findById(<span class="type">int</span>,<span class="type">int</span>))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.findById(<span class="type">int</span>,*))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.findById(*,<span class="type">int</span>))</span><br><span class="line">execution(<span class="keyword">public</span> User com.itheima.service.UserService.findById())</span><br><span class="line">execution(List com.itheima.service.*Service+.findAll(..))</span><br></pre></td></tr></table></figure>

<h3 id="3-5-切入点的三种配置方式"><a href="#3-5-切入点的三种配置方式" class="headerlink" title="3.5)切入点的三种配置方式"></a><strong>3.5)切入点的三种配置方式</strong></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--配置公共切入点--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt1&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *(..))&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--配置局部切入点--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt2&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *(..))&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--引用公共切入点--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;logAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt1&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--引用局部切入点--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;logAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt2&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--直接配置切入点--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;logAdvice&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution(* *(..))&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-6-切入点配置经验"><a href="#3-6-切入点配置经验" class="headerlink" title="3.6)切入点配置经验"></a><strong>3.6)切入点配置经验</strong></h3><ul>
<li><p>企业开发命名规范严格遵循规范文档进行</p>
</li>
<li><p>先为方法配置局部切入点</p>
</li>
<li><p>再抽取类中公共切入点</p>
</li>
<li><p>最后抽取全局切入点</p>
</li>
<li><p>代码走查过程中检测切入点是否存在越界性包含</p>
</li>
<li><p>代码走查过程中检测切入点是否存在非包含性进驻</p>
</li>
<li><p>设定AOP执行检测程序，在单元测试中监控通知被执行次数与预计次数是否匹配</p>
</li>
<li><p>设定完毕的切入点如果发生调整务必进行回归测试</p>
</li>
</ul>
<p>（以上规则适用于XML配置格式）</p>
<h3 id="3-7-通知类型"><a href="#3-7-通知类型" class="headerlink" title="3.7)通知类型"></a><strong>3.7)通知类型</strong></h3><p>AOP的通知类型共5种</p>
<ul>
<li><p>前置通知：原始方法执行前执行，如果通知中抛出异常，阻止原始方法运行</p>
<p>应用：数据校验</p>
</li>
<li><p>后置通知：原始方法执行后执行，无论原始方法中是否出现异常，都将执行通知</p>
<p>  应用：现场清理</p>
</li>
<li><p>返回后通知：原始方法正常执行完毕并返回结果后执行，如果原始方法中抛出异常，无法执行</p>
<p>  应用：返回值相关数据处理</p>
</li>
<li><p>抛出异常后通知：原始方法抛出异常后执行，如果原始方法没有抛出异常，无法执行</p>
<p>  应用：对原始方法中出现的异常信息进行处理</p>
</li>
<li><p>环绕通知：在原始方法执行前后均有对应执行执行，还可以阻止原始方法的执行</p>
<p>  应用：十分强大，可以做任何事情</p>
</li>
</ul>
<h4 id="3-7-1-aop-before"><a href="#3-7-1-aop-before" class="headerlink" title="3.7.1)aop:before"></a>3.7.1)aop:before</h4><ul>
<li><p>名称：aop:before</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:aspect标签</p>
</li>
<li><p>作用：设置前置通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;adviceId&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;methodName&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个aop:aspect标签中可以配置多个aop:before标签</p>
</li>
<li><p>基本属性：</p>
<ul>
<li><p>method ：在通知类中设置当前通知类别对应的方法</p>
</li>
<li><p>pointcut ：设置当前通知对应的切入点表达式，与pointcut-ref属性冲突</p>
</li>
<li><p>pointcut-ref ：设置当前通知对应的切入点id，与pointcut属性冲突</p>
</li>
</ul>
</li>
</ul>
<h4 id="3-7-2-aop-after"><a href="#3-7-2-aop-after" class="headerlink" title="3.7.2)aop:after"></a>3.7.2)aop:after</h4><ul>
<li><p>名称：aop:after</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:aspect标签</p>
</li>
<li><p>作用：设置后置通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;adviceId&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:after</span> <span class="attr">method</span>=<span class="string">&quot;methodName&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个aop:aspect标签中可以配置多个aop:after标签</p>
</li>
<li><p>基本属性：</p>
<ul>
<li><p>method ：在通知类中设置当前通知类别对应的方法</p>
</li>
<li><p>pointcut ：设置当前通知对应的切入点表达式，与pointcut-ref属性冲突</p>
</li>
<li><p>pointcut-ref ：设置当前通知对应的切入点id，与pointcut属性冲突</p>
</li>
</ul>
</li>
</ul>
<h4 id="3-7-3-aop-after-returning"><a href="#3-7-3-aop-after-returning" class="headerlink" title="3.7.3)aop:after-returning"></a>3.7.3)aop:after-returning</h4><ul>
<li><p>名称：aop:after-returning</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:aspect标签</p>
</li>
<li><p>作用：设置返回后通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;adviceId&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:after-returning</span> <span class="attr">method</span>=<span class="string">&quot;methodName&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个aop:aspect标签中可以配置多个aop:after-returning标签</p>
</li>
<li><p>基本属性：</p>
<ul>
<li><p>method ：在通知类中设置当前通知类别对应的方法</p>
</li>
<li><p>pointcut ：设置当前通知对应的切入点表达式，与pointcut-ref属性冲突</p>
</li>
<li><p>pointcut-ref ：设置当前通知对应的切入点id，与pointcut属性冲突</p>
</li>
</ul>
</li>
</ul>
<h4 id="3-7-4-aop-after-throwing"><a href="#3-7-4-aop-after-throwing" class="headerlink" title="3.7.4)aop:after-throwing"></a>3.7.4)aop:after-throwing</h4><ul>
<li><p>名称：aop:after-throwing</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:aspect标签</p>
</li>
<li><p>作用：设置抛出异常后通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;adviceId&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:after-throwing</span> <span class="attr">method</span>=<span class="string">&quot;methodName&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个aop:aspect标签中可以配置多个aop:after-throwing标签</p>
</li>
<li><p>基本属性：</p>
<ul>
<li><p>method ：在通知类中设置当前通知类别对应的方法</p>
</li>
<li><p>pointcut ：设置当前通知对应的切入点表达式，与pointcut-ref属性冲突</p>
</li>
<li><p>pointcut-ref ：设置当前通知对应的切入点id，与pointcut属性冲突</p>
</li>
</ul>
</li>
</ul>
<h4 id="3-7-5-aop-around"><a href="#3-7-5-aop-around" class="headerlink" title="3.7.5)aop:around"></a>3.7.5)aop:around</h4><ul>
<li><p>名称：aop:around</p>
</li>
<li><p>类型：<strong>标签</strong></p>
</li>
<li><p>归属：aop:aspect标签</p>
</li>
<li><p>作用：设置环绕通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;adviceId&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">method</span>=<span class="string">&quot;methodName&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;……&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个aop:aspect标签中可以配置多个aop:around标签</p>
</li>
<li><p>基本属性：</p>
<ul>
<li><p>method ：在通知类中设置当前通知类别对应的方法</p>
</li>
<li><p>pointcut ：设置当前通知对应的切入点表达式，与pointcut-ref属性冲突</p>
</li>
<li><p>pointcut-ref ：设置当前通知对应的切入点id，与pointcut属性冲突</p>
</li>
</ul>
</li>
</ul>
<p>环绕通知的开发方式</p>
<ul>
<li><p>环绕通知是在原始方法的前后添加功能，在环绕通知中，存在对原始方法的显式调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> Object <span class="title function_">around</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">    <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> pjp.proceed();</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>环绕通知方法相关说明：</p>
<ul>
<li><p>方法须设定Object类型的返回值，否则会拦截原始方法的返回。如果原始方法返回值类型为void，通知方	也可以设定返回值类型为void，最终返回null</p>
</li>
<li><p>方法需在第一个参数位置设定ProceedingJoinPoint对象，通过该对象调用proceed()方法，实现对原始方法的调用。如省略该参数，原始方法将无法执行</p>
</li>
<li><p>使用proceed()方法调用原始方法时，因无法预知原始方法运行过程中是否会出现异常，强制抛出Throwable对象，封装原始方法中可能出现的异常信息</p>
</li>
</ul>
</li>
</ul>
<h3 id="3-8-通知顺序（了解）"><a href="#3-8-通知顺序（了解）" class="headerlink" title="3.8)通知顺序（了解）"></a><strong>3.8)通知顺序（了解）</strong></h3><p>当同一个切入点配置了多个通知时，通知会存在运行的先后顺序，该顺序以通知配置的顺序为准</p>
<h3 id="3-9-通知获取数据"><a href="#3-9-通知获取数据" class="headerlink" title="3.9)通知获取数据"></a><strong>3.9)通知获取数据</strong></h3><ul>
<li><p>参数</p>
</li>
<li><p>返回值</p>
</li>
<li><p>异常</p>
</li>
</ul>
<h4 id="3-9-1-通知获取参数数据"><a href="#3-9-1-通知获取参数数据" class="headerlink" title="3.9.1)通知获取参数数据"></a><strong>3.9.1)通知获取参数数据</strong></h4><p>第一种情况：</p>
<ul>
<li><p>设定通知方法第一个参数为JoinPoint，通过该对象调用getArgs()方法，获取原始方法运行的参数数组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">before</span><span class="params">(JoinPoint jp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">    Object[] args = jp.getArgs();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>所有的通知均可以获取参数</p>
</li>
</ul>
<p>第二种情况：</p>
<ul>
<li><p>设定切入点表达式为通知方法传递参数（锁定通知变量名）</p>
</li>
<li><p>原始方法</p>
</li>
</ul>
<p><img src="/myl-mm/assets%5C1591284387913.png" alt="1591284387913"></p>
<p>第三种情况</p>
<ul>
<li><p>设定切入点表达式为通知方法传递参数（改变通知变量名的定义顺序）</p>
</li>
<li><p>原始方法</p>
</li>
</ul>
<p><img src="/myl-mm/assets%5C1591284429541.png" alt="1591284429541"></p>
<h4 id="3-9-2-通知获取返回值数据"><a href="#3-9-2-通知获取返回值数据" class="headerlink" title="3.9.2)通知获取返回值数据"></a><strong>3.9.2)通知获取返回值数据</strong></h4><p>第一种：返回值变量名</p>
<ul>
<li><p>设定返回值变量名</p>
</li>
<li><p>原始方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">	System.out.println(<span class="string">&quot;user service running...&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">100</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>AOP配置</p>
</li>
</ul>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt3&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *(..))  &quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:after-returning</span> <span class="attr">method</span>=<span class="string">&quot;afterReturning&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt3&quot;</span> <span class="attr">returning</span>=<span class="string">&quot;ret&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li><p>通知类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">afterReturning</span><span class="params">(Object ret)</span> &#123;</span><br><span class="line">    System.out.println(ret);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>适用于返回后通知（after-returning）</p>
</li>
</ul>
<p>第二种：</p>
<ul>
<li><p>在通知类的方法中调用原始方法获取返回值</p>
</li>
<li><p>原始方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user service running...&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">100</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>AOP配置l</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt2&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *(..))  &quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">method</span>=<span class="string">&quot;around&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt2&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>通知类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> Object <span class="title function_">around</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">    <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> pjp.proceed();</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>适用于环绕通知（around）</p>
</li>
</ul>
<h4 id="3-9-3-通知获取异常数据"><a href="#3-9-3-通知获取异常数据" class="headerlink" title="3.9.3)通知获取异常数据"></a><strong>3.9.3)通知获取异常数据</strong></h4><p>第一种：通知类的方法中调用原始方法捕获异常</p>
<ul>
<li><p>在通知类的方法中调用原始方法捕获异常</p>
</li>
<li><p>原始方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user service running...&quot;</span>);</span><br><span class="line">    <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>AOP配置</p>
</li>
</ul>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt4&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *(..))  &quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">method</span>=<span class="string">&quot;around&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt4&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li><p>通知类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> Object <span class="title function_">around</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">    <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> pjp.proceed();	<span class="comment">//对此处调用进行try……catch……捕获异常，或抛出异常</span></span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>适用于环绕通知（around）</p>
</li>
</ul>
<p>第二种：</p>
<ul>
<li><p>设定异常对象变量名</p>
</li>
<li><p>原始方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user service running...&quot;</span>);</span><br><span class="line">    <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>AOP配置</p>
</li>
</ul>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pt4&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *(..))  &quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:after-throwing</span> <span class="attr">method</span>=<span class="string">&quot;afterThrowing&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pt4&quot;</span> <span class="attr">throwing</span>=<span class="string">&quot;t&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li><p>通知类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">afterThrowing</span><span class="params">(Throwable t)</span>&#123;</span><br><span class="line">    System.out.println(t.getMessage());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>适用于返回后通知（after-throwing）</p>
</li>
</ul>
<h2 id="4-AOP配置（注解）"><a href="#4-AOP配置（注解）" class="headerlink" title="4)AOP配置（注解）"></a>4)AOP配置（注解）</h2><h3 id="4-1-AOP配置"><a href="#4-1-AOP配置" class="headerlink" title="4.1)AOP配置"></a>4.1)AOP配置</h3><p><img src="/myl-mm/assets%5C1591285050618.png" alt="1591285050618"></p>
<h3 id="4-2-注解开发AOP制作步骤"><a href="#4-2-注解开发AOP制作步骤" class="headerlink" title="4.2)注解开发AOP制作步骤"></a>4.2)注解开发AOP制作步骤</h3><p>在XML格式基础上</p>
<ul>
<li><p>导入坐标（伴随spring-context坐标导入已经依赖导入完成）</p>
</li>
<li><p>开启AOP注解支持</p>
</li>
<li><p>配置切面@Aspect</p>
</li>
<li><p>定义专用的切入点方法，并配置切入点@Pointcut</p>
</li>
<li><p>为通知方法配置通知类型及对应切入点@Before</p>
</li>
</ul>
<h3 id="4-3-注解开发AOP注意事项"><a href="#4-3-注解开发AOP注意事项" class="headerlink" title="4.3)注解开发AOP注意事项"></a>4.3)注解开发AOP注意事项</h3><p>1.切入点最终体现为一个方法，无参无返回值，无实际方法体内容，但不能是抽象方法</p>
<p>2.引用切入点时必须使用方法调用名称，方法后面的（）不能省略</p>
<p>3.切面类中定义的切入点只能在当前类中使用，如果想引用其他类中定义的切入点使用“类名.方法名()”引用</p>
<p>4.可以在通知类型注解后添加参数，实现XML配置中的属性，例如after-returning后的returning属性</p>
<p><img src="/myl-mm/assets%5C1591285152119.png" alt="1591285152119"></p>
<h3 id="4-4-AOP注解详解"><a href="#4-4-AOP注解详解" class="headerlink" title="4.4)AOP注解详解"></a>4.4)AOP注解详解</h3><h4 id="4-4-1-Aspect"><a href="#4-4-1-Aspect" class="headerlink" title="4.4.1)@Aspect"></a>4.4.1)@Aspect</h4><ul>
<li><p>名称：@Aspect</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：类定义上方</p>
</li>
<li><p>作用：设置当前类为切面类</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Aspect</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AopAdvice</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：一个beans标签中可以配置多个aop:config标签</p>
</li>
</ul>
<h4 id="4-4-2-Pointcut"><a href="#4-4-2-Pointcut" class="headerlink" title="4.4.2)@Pointcut"></a>4.4.2)@Pointcut</h4><ul>
<li><p>名称：@Pointcut</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：方法定义上方</p>
</li>
<li><p>作用：使用当前方法名作为切入点引用名称</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Pointcut(&quot;execution(* *(..))&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">pt</span><span class="params">()</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：被修饰的方法忽略其业务功能，格式设定为无参无返回值的方法，方法体内空实现（非抽象）</p>
</li>
</ul>
<h4 id="4-4-3-Before"><a href="#4-4-3-Before" class="headerlink" title="4.4.3)@Before"></a>4.4.3)@Before</h4><ul>
<li><p>名称：@Before</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：方法定义上方</p>
</li>
<li><p>作用：标注当前方法作为前置通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Before(&quot;pt()&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">before</span><span class="params">()</span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>特殊参数：</p>
<ul>
<li>无</li>
</ul>
</li>
</ul>
<h4 id="4-4-4-After"><a href="#4-4-4-After" class="headerlink" title="4.4.4)@After"></a>4.4.4)@After</h4><ul>
<li><p>名称：@After</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：方法定义上方</p>
</li>
<li><p>作用：标注当前方法作为后置通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@After(&quot;pt()&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">after</span><span class="params">()</span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>特殊参数：</p>
<ul>
<li>无</li>
</ul>
</li>
</ul>
<h4 id="4-4-5-AfterReturning"><a href="#4-4-5-AfterReturning" class="headerlink" title="4.4.5)@AfterReturning"></a>4.4.5)@AfterReturning</h4><ul>
<li><p>名称：@AfterReturning</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：方法定义上方</p>
</li>
<li><p>作用：标注当前方法作为返回后通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@AfterReturning(value=&quot;pt()&quot;,returning = &quot;ret&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">afterReturning</span><span class="params">(Object ret)</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>特殊参数：</p>
<ul>
<li>returning ：设定使用通知方法参数接收返回值的变量名</li>
</ul>
</li>
</ul>
<h4 id="4-4-6-AfterThrowing"><a href="#4-4-6-AfterThrowing" class="headerlink" title="4.4.6)@AfterThrowing"></a>4.4.6)@AfterThrowing</h4><ul>
<li><p>名称：@AfterThrowing</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：方法定义上方</p>
</li>
<li><p>作用：标注当前方法作为异常后通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@AfterThrowing(value=&quot;pt()&quot;,throwing = &quot;t&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">afterThrowing</span><span class="params">(Throwable t)</span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>特殊参数：</p>
<ul>
<li>throwing ：设定使用通知方法参数接收原始方法中抛出的异常对象名</li>
</ul>
</li>
</ul>
<h4 id="4-4-7-Around"><a href="#4-4-7-Around" class="headerlink" title="4.4.7)@Around"></a>4.4.7)@Around</h4><ul>
<li><p>名称：@Around</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：方法定义上方</p>
</li>
<li><p>作用：标注当前方法作为环绕通知</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Around(&quot;pt()&quot;)</span></span><br><span class="line"><span class="keyword">public</span> Object <span class="title function_">around</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">    <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> pjp.proceed();</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>特殊参数：</p>
<ul>
<li>无</li>
</ul>
</li>
</ul>
<h3 id="4-5-AOP注解开发通知执行顺序控制（了解）"><a href="#4-5-AOP注解开发通知执行顺序控制（了解）" class="headerlink" title="4.5)AOP注解开发通知执行顺序控制（了解）"></a>4.5)AOP注解开发通知执行顺序控制（了解）</h3><p>1.AOP使用XML配置情况下，通知的执行顺序由配置顺序决定，在注解情况下由于不存在配置顺序的概念的概念，参照通知所配置的方法名字符串对应的编码值顺序，可以简单理解为字母排序</p>
<ul>
<li><p>同一个通知类中，相同通知类型以方法名排序为准</p>
</li>
<li><p>不同通知类中，以类名排序为准</p>
</li>
<li><p>使用@Order注解通过变更bean的加载顺序改变通知的加载顺序</p>
</li>
</ul>
<p>2.企业开发经验</p>
<ul>
<li><p>通知方法名由3部分组成，分别是前缀、顺序编码、功能描述</p>
</li>
<li><p>前缀为固定字符串，例如baidu、itheima等，无实际意义</p>
</li>
<li><p>顺序编码为6位以内的整数，通常3位即可，不足位补0</p>
</li>
<li><p>功能描述为该方法对应的实际通知功能，例如exception、strLenCheck</p>
<ul>
<li><p>制通知执行顺序使用顺序编码控制，使用时做一定空间预留</p>
</li>
<li><p>003使用，006使用，预留001、002、004、005、007、008</p>
</li>
<li><p>使用时从中段开始使用，方便后期做前置追加或后置追加</p>
</li>
<li><p>最终顺序以运行顺序为准，以测试结果为准，不以设定规则为准</p>
</li>
</ul>
</li>
</ul>
<h3 id="4-6-AOP注解驱动"><a href="#4-6-AOP注解驱动" class="headerlink" title="4.6)AOP注解驱动"></a>4.6)AOP注解驱动</h3><ul>
<li><p>名称：@EnableAspectJAutoProxy</p>
</li>
<li><p>类型：<strong>注解</strong></p>
</li>
<li><p>位置：Spring注解配置类定义上方</p>
</li>
<li><p>作用：设置当前类开启AOP注解驱动的支持，加载AOP注解</p>
</li>
<li><p>格式：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ComponentScan(&quot;com.itheima&quot;)</span></span><br><span class="line"><span class="meta">@EnableAspectJAutoProxy</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SpringConfig</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="5-综合案例"><a href="#5-综合案例" class="headerlink" title="5)综合案例"></a>5)综合案例</h2><h3 id="5-1-案例介绍"><a href="#5-1-案例介绍" class="headerlink" title="5.1)案例介绍"></a><strong>5.1)案例介绍</strong></h3><p>对项目进行业务层接口执行监控，测量业务层接口的执行效率</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">AccountService</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">save</span><span class="params">(Account account)</span>;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">delete</span><span class="params">(Integer id)</span>;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">update</span><span class="params">(Account account)</span>;</span><br><span class="line">    List&lt;Account&gt; <span class="title function_">findAll</span><span class="params">()</span>;</span><br><span class="line">    Account <span class="title function_">findById</span><span class="params">(Integer id)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-2-案例分析"><a href="#5-2-案例分析" class="headerlink" title="5.2)案例分析"></a><strong>5.2)案例分析</strong></h3><ul>
<li><p>测量接口执行效率：接口方法执行前后获取执行时间，求出执行时长</p>
<ul>
<li>System.currentTimeMillis( )</li>
</ul>
</li>
<li><p>对项目进行监控：项目中所有接口方法，AOP思想，执行期动态织入代码</p>
<ul>
<li><p>环绕通知</p>
</li>
<li><p>proceed()方法执行前后获取系统时间</p>
</li>
</ul>
</li>
</ul>
<h3 id="5-3-案例制作步骤"><a href="#5-3-案例制作步骤" class="headerlink" title="5.3)案例制作步骤"></a><strong>5.3)案例制作步骤</strong></h3><ul>
<li><p>定义切入点（务必要绑定到接口上，而不是接口实现类上）</p>
</li>
<li><p>制作AOP环绕通知，完成测量功能</p>
</li>
<li><p>注解配置AOP</p>
</li>
<li><p>开启注解驱动支持</p>
</li>
</ul>
<h3 id="5-4-案例制作核心代码"><a href="#5-4-案例制作核心代码" class="headerlink" title="5.4)案例制作核心代码"></a><strong>5.4)案例制作核心代码</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RunTimeMonitorAdvice</span> &#123;</span><br><span class="line">    <span class="comment">//拦截所有的业务层接口中查询操作的执行</span></span><br><span class="line">    <span class="meta">@Pointcut(&quot;execution(* com.itheima.service.*Service.find*(..))&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">pt</span><span class="params">()</span>&#123;&#125;</span><br><span class="line">    <span class="meta">@Around(&quot;pt()&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> Object <span class="title function_">runtimeMonitor</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">        <span class="comment">//获取执行签名信息</span></span><br><span class="line">        <span class="type">Signature</span> <span class="variable">signature</span> <span class="operator">=</span> pjp.getSignature();</span><br><span class="line">        <span class="comment">//通过签名获取执行类型（接口名）</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">targetClass</span> <span class="operator">=</span> signature.getDeclaringTypeName();</span><br><span class="line">        <span class="comment">//通过签名获取执行操作名称（方法名）</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">targetMethod</span> <span class="operator">=</span> signature.getName();</span><br><span class="line">        <span class="comment">//获取操作前系统时间beginTime</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">beginTime</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> pjp.proceed(pjp.getArgs());</span><br><span class="line">        <span class="comment">//获取操作后系统时间endTime</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">endTime</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        System.out.println(targetClass+<span class="string">&quot; 中 &quot;</span>+targetMethod+<span class="string">&quot; 运行时长 &quot;</span>+(endTime-beginTime)+<span class="string">&quot;ms&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> ret;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-5-案例后续思考与设计"><a href="#5-5-案例后续思考与设计" class="headerlink" title="5.5)案例后续思考与设计"></a><strong>5.5)案例后续思考与设计</strong></h3><ul>
<li><p>测量真实性</p>
<ul>
<li><p>开发测量是隔离性反复执行某个操作，是理想情况，上线测量差异过大</p>
</li>
<li><p>上线测量服务器性能略低于单机开发测量</p>
</li>
<li><p>上线测量基于缓存的性能查询要优于数据库查询测量</p>
</li>
<li><p>上线测量接口的性能与最终对外提供的服务性能差异过大</p>
</li>
<li><p>当外部条件发生变化（硬件），需要进行回归测试，例如数据库迁移</p>
</li>
</ul>
</li>
<li><p>测量结果展示</p>
<ul>
<li><p>测量结果无需每一个都展示，需要设定检测阈值</p>
</li>
<li><p>阈值设定要根据业务进行区分，一个复杂的查询与简单的查询差异化很大</p>
</li>
<li><p>阈值设定需要做独立的配置文件或通过图形工具配置（工具级别的开发）</p>
</li>
<li><p>配合图形界面展示测量结果</p>
</li>
</ul>
</li>
</ul>
<h2 id="6-AOP底层原理"><a href="#6-AOP底层原理" class="headerlink" title="6)AOP底层原理"></a>6)AOP底层原理</h2><ul>
<li><p>静态代理</p>
</li>
<li><p>动态代理——Proxy</p>
</li>
<li><p>动态代理——CGLIB</p>
</li>
<li><p>织入形式</p>
</li>
</ul>
<h3 id="6-1-静态代理"><a href="#6-1-静态代理" class="headerlink" title="6.1)静态代理"></a><strong>6.1)静态代理</strong></h3><p>装饰者模式（Decorator Pattern）：在不惊动原始设计的基础上，为其添加功能</p>
<p><img src="/myl-mm/assets%5C1591287238806.png" alt="1591287238806"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserServiceDecorator</span> <span class="keyword">implements</span> <span class="title class_">UserService</span>&#123;</span><br><span class="line">    <span class="keyword">private</span> UserService userService;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">UserServiceDecorator</span><span class="params">(UserService userService)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.userService = userService;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//原始调用</span></span><br><span class="line">        userService.save();</span><br><span class="line">        <span class="comment">//增强功能（后置）</span></span><br><span class="line">        System.out.println(<span class="string">&quot;刮大白&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-2-动态代理——JDK-Proxy"><a href="#6-2-动态代理——JDK-Proxy" class="headerlink" title="6.2)动态代理——JDK Proxy"></a>6.2)动态代理——JDK Proxy</h3><p>JDKProxy动态代理是针对对象做代理，要求原始对象具有接口实现，并对接口方法进行增强</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserServiceJDKProxy</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> UserService <span class="title function_">createUserServiceJDKProxy</span><span class="params">(<span class="keyword">final</span> UserService userService)</span>&#123;</span><br><span class="line">        <span class="comment">//获取被代理对象的类加载器</span></span><br><span class="line">        <span class="type">ClassLoader</span> <span class="variable">classLoader</span> <span class="operator">=</span> userService.getClass().getClassLoader();</span><br><span class="line">        <span class="comment">//获取被代理对象实现的接口</span></span><br><span class="line">        Class[] classes = userService.getClass().getInterfaces();</span><br><span class="line">        <span class="comment">//对原始方法执行进行拦截并增强</span></span><br><span class="line">        <span class="type">InvocationHandler</span> <span class="variable">ih</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">InvocationHandler</span>() &#123;</span><br><span class="line">            <span class="keyword">public</span> Object <span class="title function_">invoke</span><span class="params">(Object proxy, Method method, Object[] args)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">                <span class="comment">//前置增强内容</span></span><br><span class="line">                <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> method.invoke(userService, args);</span><br><span class="line">                <span class="comment">//后置增强内容</span></span><br><span class="line">                System.out.println(<span class="string">&quot;刮大白2&quot;</span>);</span><br><span class="line">                <span class="keyword">return</span> ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="comment">//使用原始被代理对象创建新的代理对象</span></span><br><span class="line">        <span class="type">UserService</span> <span class="variable">proxy</span> <span class="operator">=</span> (UserService) Proxy.newProxyInstance(classLoader,classes,ih);</span><br><span class="line">        <span class="keyword">return</span> proxy;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-3-动态代理——CGLIB"><a href="#6-3-动态代理——CGLIB" class="headerlink" title="6.3)动态代理——CGLIB"></a>6.3)动态代理——CGLIB</h3><ul>
<li><p>CGLIB(Code Generation Library)，Code生成类库</p>
</li>
<li><p>CGLIB动态代理不限定是否具有接口，可以对任意操作进行增强</p>
</li>
<li><p>CGLIB动态代理无需要原始被代理对象，动态创建出新的代理对象</p>
</li>
</ul>
<p><img src="/myl-mm/assets%5C1591287441096.png" alt="1591287441096"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserServiceImplCglibProxy</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> UserServiceImpl <span class="title function_">createUserServiceCglibProxy</span><span class="params">(Class clazz)</span>&#123;</span><br><span class="line">        <span class="comment">//创建Enhancer对象（可以理解为内存中动态创建了一个类的字节码）</span></span><br><span class="line">        <span class="type">Enhancer</span> <span class="variable">enhancer</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Enhancer</span>();</span><br><span class="line">        <span class="comment">//设置Enhancer对象的父类是指定类型UserServerImpl</span></span><br><span class="line">        enhancer.setSuperclass(clazz);</span><br><span class="line">        <span class="type">Callback</span> <span class="variable">cb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MethodInterceptor</span>() &#123;</span><br><span class="line">            <span class="keyword">public</span> Object <span class="title function_">intercept</span><span class="params">(Object o, Method m, Object[] a, MethodProxy mp)</span> <span class="keyword">throws</span> Throwable &#123;</span><br><span class="line">                <span class="type">Object</span> <span class="variable">ret</span> <span class="operator">=</span> mp.invokeSuper(o, a);</span><br><span class="line">                <span class="keyword">if</span>(m.getName().equals(<span class="string">&quot;save&quot;</span>)) &#123;</span><br><span class="line">                    System.out.println(<span class="string">&quot;刮大白&quot;</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="comment">//设置回调方法</span></span><br><span class="line">        enhancer.setCallback(cb);</span><br><span class="line">        <span class="comment">//使用Enhancer对象创建对应的对象</span></span><br><span class="line">        <span class="keyword">return</span> (UserServiceImpl)enhancer.create();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-4-代理模式的选择"><a href="#6-4-代理模式的选择" class="headerlink" title="6.4)代理模式的选择"></a><strong>6.4)代理模式的选择</strong></h3><p>Spirng可以通过配置的形式控制使用的代理形式，默认使用jdkproxy，通过配置可以修改为使用cglib</p>
<ul>
<li><p>XML配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--XMP配置AOP--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">aop:config</span> <span class="attr">proxy-target-class</span>=<span class="string">&quot;false&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>XML注解支持</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--注解配置AOP--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">aop:aspectj-autoproxy</span> <span class="attr">proxy-target-class</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>注解驱动</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//注解驱动</span></span><br><span class="line"><span class="meta">@EnableAspectJAutoProxy(proxyTargetClass = true)</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="6-5-织入时机"><a href="#6-5-织入时机" class="headerlink" title="6.5)织入时机"></a><strong>6.5)织入时机</strong></h3><p><img src="/myl-mm/assets%5C1591287839941.png" alt="1591287839941"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/tgreen/myl-mm.git">myl</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/tgreen/myl-mm.git/2022/05/09/Spring-day03/">https://gitee.com/tgreen/myl-mm.git/2022/05/09/Spring-day03/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/tgreen/myl-mm.git" target="_blank">阿毛的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/myl-mm/tags/spring/">spring</a></div><div class="post_share"><div class="social-share" data-image="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/myl-mm/2022/05/09/spring-day04/"><img class="prev-cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="onerror=null;src='/myl-mm/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Spring-day04</div></div></a></div><div class="next-post pull-right"><a href="/myl-mm/2022/05/09/Spring-day02/"><img class="next-cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="onerror=null;src='/myl-mm/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Spring-day02</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/myl-mm/2022/05/09/Spring-day02/" title="Spring-day02"><img class="cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-09</div><div class="title">Spring-day02</div></div></a></div><div><a href="/myl-mm/2022/05/09/spring-day04/" title="Spring-day04"><img class="cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-09</div><div class="title">Spring-day04</div></div></a></div><div><a href="/myl-mm/2022/05/09/Spring-day01/" title="Spring-day01"><img class="cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-09</div><div class="title">Spring-day01</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/myl-mm/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">myl</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/myl-mm/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/myl-mm/tags/"><div class="headline">标签</div><div class="length-num">11</div></a><a href="/myl-mm/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><a id="card-info-btn" href="https://gitee.com/tgreen/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎观看，又来一个！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#AOP"><span class="toc-number">1.</span> <span class="toc-text">AOP</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-AOP%E7%AE%80%E4%BB%8B"><span class="toc-number">1.1.</span> <span class="toc-text">1)AOP简介</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-OOP%E5%BC%80%E5%8F%91%E6%80%9D%E8%B7%AF"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.1)OOP开发思路</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-AOP%E5%BC%80%E5%8F%91%E6%80%9D%E6%83%B3"><span class="toc-number">1.1.2.</span> <span class="toc-text">1.2)AOP开发思想</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-AOP%E6%A6%82%E5%BF%B5"><span class="toc-number">1.1.3.</span> <span class="toc-text">1.3)AOP概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-AOP%E4%BD%9C%E7%94%A8"><span class="toc-number">1.1.4.</span> <span class="toc-text">1.4)AOP作用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-5-AOP%E4%BC%98%E5%8A%BF"><span class="toc-number">1.1.5.</span> <span class="toc-text">1.5)AOP优势</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-AOP%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B"><span class="toc-number">1.2.</span> <span class="toc-text">2)AOP入门案例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-AOP%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5"><span class="toc-number">1.2.1.</span> <span class="toc-text">2.1)AOP相关概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-AOP%E5%BC%80%E5%8F%91%E8%BF%87%E7%A8%8B"><span class="toc-number">1.2.2.</span> <span class="toc-text">2.2)AOP开发过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-AOP%E5%BC%80%E5%8F%91%E6%96%B9%E5%BC%8F"><span class="toc-number">1.2.3.</span> <span class="toc-text">2.2)AOP开发方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%E5%88%B6%E4%BD%9C%E5%88%86%E6%9E%90"><span class="toc-number">1.2.4.</span> <span class="toc-text">2.3)入门案例制作分析</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-AOP%E9%85%8D%E7%BD%AE%EF%BC%88XML%EF%BC%89"><span class="toc-number">1.3.</span> <span class="toc-text">3)AOP配置（XML）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-AspectJ"><span class="toc-number">1.3.1.</span> <span class="toc-text">3.1)AspectJ</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-AOP%E9%85%8D%E7%BD%AE"><span class="toc-number">1.3.2.</span> <span class="toc-text">3.2)AOP配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-1-aop-config"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">3.2.1)aop:config</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-2-aop-aspect"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">3.2.2)aop:aspect</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-3-aop-pointcut"><span class="toc-number">1.3.2.3.</span> <span class="toc-text">3.2.3)aop:pointcut</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-%E5%88%87%E5%85%A5%E7%82%B9"><span class="toc-number">1.3.3.</span> <span class="toc-text">3.3)切入点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-%E5%88%87%E5%85%A5%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E7%BB%84%E6%88%90"><span class="toc-number">1.3.4.</span> <span class="toc-text">3.4)切入点表达式的组成</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-1-%E5%88%87%E5%85%A5%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F%E2%80%94%E2%80%94%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.3.4.1.</span> <span class="toc-text">3.4.1)切入点表达式——关键字</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-2-%E5%88%87%E5%85%A5%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F%E2%80%94%E2%80%94%E9%80%9A%E9%85%8D%E7%AC%A6"><span class="toc-number">1.3.4.2.</span> <span class="toc-text">3.4.2)切入点表达式——通配符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-3-%E5%88%87%E5%85%A5%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F%E2%80%94%E2%80%94%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.3.4.3.</span> <span class="toc-text">3.4.3)切入点表达式——逻辑运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-4-%E5%88%87%E5%85%A5%E7%82%B9%E8%A1%A8%E8%BE%BE%E5%BC%8F%E2%80%94%E2%80%94%E8%8C%83%E4%BE%8B"><span class="toc-number">1.3.4.4.</span> <span class="toc-text">3.4.4)切入点表达式——范例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-5-%E5%88%87%E5%85%A5%E7%82%B9%E7%9A%84%E4%B8%89%E7%A7%8D%E9%85%8D%E7%BD%AE%E6%96%B9%E5%BC%8F"><span class="toc-number">1.3.5.</span> <span class="toc-text">3.5)切入点的三种配置方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-6-%E5%88%87%E5%85%A5%E7%82%B9%E9%85%8D%E7%BD%AE%E7%BB%8F%E9%AA%8C"><span class="toc-number">1.3.6.</span> <span class="toc-text">3.6)切入点配置经验</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-7-%E9%80%9A%E7%9F%A5%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.7.</span> <span class="toc-text">3.7)通知类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-1-aop-before"><span class="toc-number">1.3.7.1.</span> <span class="toc-text">3.7.1)aop:before</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-2-aop-after"><span class="toc-number">1.3.7.2.</span> <span class="toc-text">3.7.2)aop:after</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-3-aop-after-returning"><span class="toc-number">1.3.7.3.</span> <span class="toc-text">3.7.3)aop:after-returning</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-4-aop-after-throwing"><span class="toc-number">1.3.7.4.</span> <span class="toc-text">3.7.4)aop:after-throwing</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-5-aop-around"><span class="toc-number">1.3.7.5.</span> <span class="toc-text">3.7.5)aop:around</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-8-%E9%80%9A%E7%9F%A5%E9%A1%BA%E5%BA%8F%EF%BC%88%E4%BA%86%E8%A7%A3%EF%BC%89"><span class="toc-number">1.3.8.</span> <span class="toc-text">3.8)通知顺序（了解）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-9-%E9%80%9A%E7%9F%A5%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-number">1.3.9.</span> <span class="toc-text">3.9)通知获取数据</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-1-%E9%80%9A%E7%9F%A5%E8%8E%B7%E5%8F%96%E5%8F%82%E6%95%B0%E6%95%B0%E6%8D%AE"><span class="toc-number">1.3.9.1.</span> <span class="toc-text">3.9.1)通知获取参数数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-2-%E9%80%9A%E7%9F%A5%E8%8E%B7%E5%8F%96%E8%BF%94%E5%9B%9E%E5%80%BC%E6%95%B0%E6%8D%AE"><span class="toc-number">1.3.9.2.</span> <span class="toc-text">3.9.2)通知获取返回值数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-3-%E9%80%9A%E7%9F%A5%E8%8E%B7%E5%8F%96%E5%BC%82%E5%B8%B8%E6%95%B0%E6%8D%AE"><span class="toc-number">1.3.9.3.</span> <span class="toc-text">3.9.3)通知获取异常数据</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-AOP%E9%85%8D%E7%BD%AE%EF%BC%88%E6%B3%A8%E8%A7%A3%EF%BC%89"><span class="toc-number">1.4.</span> <span class="toc-text">4)AOP配置（注解）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-AOP%E9%85%8D%E7%BD%AE"><span class="toc-number">1.4.1.</span> <span class="toc-text">4.1)AOP配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E6%B3%A8%E8%A7%A3%E5%BC%80%E5%8F%91AOP%E5%88%B6%E4%BD%9C%E6%AD%A5%E9%AA%A4"><span class="toc-number">1.4.2.</span> <span class="toc-text">4.2)注解开发AOP制作步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E6%B3%A8%E8%A7%A3%E5%BC%80%E5%8F%91AOP%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">1.4.3.</span> <span class="toc-text">4.3)注解开发AOP注意事项</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-AOP%E6%B3%A8%E8%A7%A3%E8%AF%A6%E8%A7%A3"><span class="toc-number">1.4.4.</span> <span class="toc-text">4.4)AOP注解详解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-1-Aspect"><span class="toc-number">1.4.4.1.</span> <span class="toc-text">4.4.1)@Aspect</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-2-Pointcut"><span class="toc-number">1.4.4.2.</span> <span class="toc-text">4.4.2)@Pointcut</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-3-Before"><span class="toc-number">1.4.4.3.</span> <span class="toc-text">4.4.3)@Before</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-4-After"><span class="toc-number">1.4.4.4.</span> <span class="toc-text">4.4.4)@After</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-5-AfterReturning"><span class="toc-number">1.4.4.5.</span> <span class="toc-text">4.4.5)@AfterReturning</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-6-AfterThrowing"><span class="toc-number">1.4.4.6.</span> <span class="toc-text">4.4.6)@AfterThrowing</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-7-Around"><span class="toc-number">1.4.4.7.</span> <span class="toc-text">4.4.7)@Around</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-AOP%E6%B3%A8%E8%A7%A3%E5%BC%80%E5%8F%91%E9%80%9A%E7%9F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F%E6%8E%A7%E5%88%B6%EF%BC%88%E4%BA%86%E8%A7%A3%EF%BC%89"><span class="toc-number">1.4.5.</span> <span class="toc-text">4.5)AOP注解开发通知执行顺序控制（了解）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-6-AOP%E6%B3%A8%E8%A7%A3%E9%A9%B1%E5%8A%A8"><span class="toc-number">1.4.6.</span> <span class="toc-text">4.6)AOP注解驱动</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B"><span class="toc-number">1.5.</span> <span class="toc-text">5)综合案例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E6%A1%88%E4%BE%8B%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.5.1.</span> <span class="toc-text">5.1)案例介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-%E6%A1%88%E4%BE%8B%E5%88%86%E6%9E%90"><span class="toc-number">1.5.2.</span> <span class="toc-text">5.2)案例分析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-%E6%A1%88%E4%BE%8B%E5%88%B6%E4%BD%9C%E6%AD%A5%E9%AA%A4"><span class="toc-number">1.5.3.</span> <span class="toc-text">5.3)案例制作步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-%E6%A1%88%E4%BE%8B%E5%88%B6%E4%BD%9C%E6%A0%B8%E5%BF%83%E4%BB%A3%E7%A0%81"><span class="toc-number">1.5.4.</span> <span class="toc-text">5.4)案例制作核心代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-5-%E6%A1%88%E4%BE%8B%E5%90%8E%E7%BB%AD%E6%80%9D%E8%80%83%E4%B8%8E%E8%AE%BE%E8%AE%A1"><span class="toc-number">1.5.5.</span> <span class="toc-text">5.5)案例后续思考与设计</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-AOP%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86"><span class="toc-number">1.6.</span> <span class="toc-text">6)AOP底层原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-%E9%9D%99%E6%80%81%E4%BB%A3%E7%90%86"><span class="toc-number">1.6.1.</span> <span class="toc-text">6.1)静态代理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-2-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E2%80%94%E2%80%94JDK-Proxy"><span class="toc-number">1.6.2.</span> <span class="toc-text">6.2)动态代理——JDK Proxy</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3-%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E2%80%94%E2%80%94CGLIB"><span class="toc-number">1.6.3.</span> <span class="toc-text">6.3)动态代理——CGLIB</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-4-%E4%BB%A3%E7%90%86%E6%A8%A1%E5%BC%8F%E7%9A%84%E9%80%89%E6%8B%A9"><span class="toc-number">1.6.4.</span> <span class="toc-text">6.4)代理模式的选择</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-5-%E7%BB%87%E5%85%A5%E6%97%B6%E6%9C%BA"><span class="toc-number">1.6.5.</span> <span class="toc-text">6.5)织入时机</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/RabbitMQ%20Windows%20%E5%AE%89%E8%A3%85/" title="RabbitMQ Windows 安装"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="RabbitMQ Windows 安装"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/RabbitMQ%20Windows%20%E5%AE%89%E8%A3%85/" title="RabbitMQ Windows 安装">RabbitMQ Windows 安装</a><time datetime="2022-05-09T09:04:30.600Z" title="发表于 2022-05-09 17:04:30">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/gateway%E5%86%85%E7%BD%AE%E8%BF%87%E6%BB%A4%E5%99%A8%E5%B7%A5%E5%8E%82/" title="Spring Cloud Gateway 内置的过滤器工厂"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="Spring Cloud Gateway 内置的过滤器工厂"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/gateway%E5%86%85%E7%BD%AE%E8%BF%87%E6%BB%A4%E5%99%A8%E5%B7%A5%E5%8E%82/" title="Spring Cloud Gateway 内置的过滤器工厂">Spring Cloud Gateway 内置的过滤器工厂</a><time datetime="2022-05-09T09:04:19.065Z" title="发表于 2022-05-09 17:04:19">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/Turbine%E6%90%AD%E5%BB%BA%E6%AD%A5%E9%AA%A4/" title="Turbine"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="Turbine"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/Turbine%E6%90%AD%E5%BB%BA%E6%AD%A5%E9%AA%A4/" title="Turbine">Turbine</a><time datetime="2022-05-09T09:04:13.000Z" title="发表于 2022-05-09 17:04:13">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/Eureka%20%E5%85%A8%E9%83%A8%E9%85%8D%E7%BD%AE%20/" title="Euraka配置详解"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="Euraka配置详解"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/Eureka%20%E5%85%A8%E9%83%A8%E9%85%8D%E7%BD%AE%20/" title="Euraka配置详解">Euraka配置详解</a><time datetime="2022-05-09T09:03:49.699Z" title="发表于 2022-05-09 17:03:49">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/01-Springboot-day01/" title="springBootday01"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="springBootday01"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/01-Springboot-day01/" title="springBootday01">springBootday01</a><time datetime="2022-05-09T09:03:15.476Z" title="发表于 2022-05-09 17:03:15">2022-05-09</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By myl</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/myl-mm/js/utils.js"></script><script src="/myl-mm/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>